/*****************************************************************************
 * main.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE "mqtt.main"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void dev_mqtt_msg_handle(plum_mqtt_evt_t evt, plum_void *arg)
{
    switch(evt){
        case MQTT_EVT_MSG:
            {
                plum_mqtt_msg_t *msg = (plum_mqtt_msg_t *)arg;
                LOG_D("recv topic : %s",msg->topic);
                LOG_D("recv payload : %s",msg->payload);
            }
            break;

        default: 
            LOG_E("evt[%d] not support",evt);
            break;
    }
}
/*****************************************************************************
 * Public Functions
 ****************************************************************************/

int main(void)
{
    plum_mqtt_client_ref_t client = plum_cal_mqtt_connect("T192E2AXRN10103CHB510013001",
                        "T192E2AXRN.iotcloud.tencentdevices.com",
                        1883,
                        "T192E2AXRN10103CHB510013001;12010126;3N1HE;1799128947",
                        "6efbf19da02dfdd938ed3b036eb74a9997ed0699493fc20f51c9a2e90bc76669;hmacsha256");
    if(!client){
        LOG_E("plum_cal_mqtt_connect err");
    }
    LOG_D("plum_cal_mqtt_connect");

    int rc = plum_cal_mqtt_subscribe(client,"T192E2AXRN/10103CHB510013001/down/control");
    if(rc){
        LOG_E("plum_cal_mqtt_subscribe err,rc : %d",rc);
    }
    LOG_D("plum_cal_mqtt_subscribe");

    rc = plum_cal_mqtt_register_callback(dev_mqtt_msg_handle);
    if(rc){
        LOG_E("plum_cal_mqtt_register_callback err,rc : %d",rc);
    }
    LOG_D("plum_cal_mqtt_register_callback");

    while(1){
        plum_kal_thread_delay(1000);
    }
}
/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
